package cn.admin.remoting;

import cn.admin.remoting.serialize.SerializationUtil;
import cn.admin.remoting.spi.SessionsFlushQueueService;

import java.lang.reflect.InvocationTargetException;

public class Remoting {

    private static ServerManager sm;
    private static SessionsFlushQueueService spi;
    static transient boolean isCloseWhenIdleTimeout = true;

    public static ServerManager serverMgr() {
        return sm;
    }

    public static ClientManager clientMgr() {
        return ClientManager.getImpl();
    }

    public static SerializationUtil serialUtil() {
        return SerializationUtil.getImpl();
    }

    public static SessionsFlushQueueService sessionsFlushQueueService() {
        return spi;
    }

    public static void setIsCloseWhenIdleTimeout(boolean val) {
        isCloseWhenIdleTimeout = val;
    }

    public static boolean isCloseWhenIdleTimeout() {
        return isCloseWhenIdleTimeout;
    }

    static {
        if (sm == null) {
            try {
                Class.forName("cn.admin.remoting.impl.OnDeploy").
                        getDeclaredMethod("onDeploy").invoke(null);
            } catch (IllegalAccessException | InvocationTargetException
                    | NoSuchMethodException | ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
    }

}
